using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Read.Models;
using Read.Services;
using Read.Utility;

namespace Read.Web.Admin
{
    /// <summary>
    /// 节点管理
    /// </summary>
    public class NodeController : AdminBaseController
    {
        /// <summary>
        /// 节点列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(int roleId = 1)
        {
            if (HttpContext.Request.IsAjaxRequest())
            {
                var srevice = new NodeService();
                var list = srevice.getMenuTree(roleId, false);
                var res = new ResultData<ResultMenuTreeModel>()
                {
                    Result = true,
                    Data = list
                };
                return Json(res);
            }
            else
            {
                ViewBag.Title = "节点列表";
                return View();
            }
        }
        
        /// <summary>
        /// 节点添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(NodeModel model)
        {
            var srevice = new NodeService();
            var add = srevice.nodeAdd(model);
            var res = new ResultData<int>()
            {
                Result = true,
                Data = add,
                Message = "添加节点成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }

        /// <summary>
        /// 节点编辑
        /// </summary>
        /// <returns></returns>
        [HttpPut, HttpGet]
        public IActionResult Edit(NodeModel model)
        {
            string method = Request.Method.ToUpper();
            if (method == "GET")
            {
                var srevice = new NodeService();
                var list = srevice.getNodeById(model.Id);
                var res = new ResultData<ResultNodeModel>()
                {
                    Result = true,
                    Data = list,
                    Message = "查询成功"
                };
                return Json(res);
            }
            else
            {
                var srevice = new NodeService();
                var edit = srevice.nodeEdit(model);
                var res = new ResultData<bool>()
                {
                    Result = true,
                    Data = edit,
                    Message = "编辑节点成功"
                };
                return ResultJSON(res.Result, res, res.Message);
            }
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int id)
        {
            var srevice = new NodeService();
            var del = srevice.nodeDel(id);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = true,
                Message = "删除节点成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }
    }
}